//
//  SideBarViewController2.m
//  老司机发车了
//
//  Created by 斌 on 16/8/10.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "SideBarViewController2.h"
#import "EditInfoTableViewController.h"
@interface SideBarViewController2 ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UIImageView *backimg;
@property (strong, nonatomic) UIImageView *imgview;
@property (strong, nonatomic) UILabel *nickname;
@property (strong, nonatomic) UILabel *level;
@property (strong, nonatomic) UILabel *sign;
@property (strong, nonatomic) UITableView *tableview;

@property (nonatomic,strong) NSArray *titleArray;

@end

@implementation SideBarViewController2

- (void)viewWillAppear:(BOOL)animated {
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi" object:nil];
   
    
}

// 通知方法
- (void)tongzhi:(NSNotification *)notification{
    
    self.nickname.text = notification.userInfo[@"nickname"];
    NSString *str = notification.userInfo[@"imgUrl"];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
    UIImage *img = [UIImage imageWithData:data];
    self.imgview.image = img;
    self.sign.text = notification.userInfo[@"sign"];
    self.level.text = notification.userInfo[@"level"];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // 背景
    self.backimg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_backimg];
    _backimg.image = [UIImage imageNamed:@"登陆界面.jpg"];
    
    self.imgview = [[UIImageView alloc] initWithFrame:CGRectMake(30, 100, 80, 80)];
    [self.view addSubview:_imgview];
    // 昵称
    self.nickname = [[UILabel alloc] initWithFrame:CGRectMake(_imgview.endX + 10, _imgview.ownY, 150, 30)];
    [self.view addSubview:_nickname];
    // 等级
    self.level = [[UILabel alloc] initWithFrame:CGRectMake(_nickname.ownX, _imgview.endY - 30, 150, 30)];
    [self.view addSubview:_level];
    // 个性签名
    self.sign = [[UILabel alloc] initWithFrame:CGRectMake(_imgview.ownX, _imgview.endY + 10, _level.endX - _imgview.ownX, 60)];
    [self.view addSubview:_sign];
    
    // tableview
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(_imgview.ownX, _sign.endY + 20, _sign.ownWidth, 300) style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.titleArray = @[@"编辑资料",@"我的相册",@"退出登陆",@"设置"];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
}

// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        EditInfoTableViewController *VC = [[EditInfoTableViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
        [self presentViewController:nav animated:YES completion:nil];
    }
    if (indexPath.row == 2) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"大王真的要离我而去吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [macro shareInstance].isLogin = NO;
            [self.parentViewController transitionFromViewController:self toViewController:self.parentViewController.childViewControllers[0] duration:0.1 options:UIViewAnimationOptionTransitionNone animations:nil completion:nil];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:sure];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = _titleArray[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
